﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using bzxxxt.admin.entity;
using bzxxxt.dal;
using bzxxxt.model;
namespace bzxxxt.admin.service
{
    public class MoveHisService : BaseService
    {
        UserEntity user = UserService.GetUser(HttpContext.Current);
        public string getall()
        {
            var list = from b in DB.tbMoveHis                       
                       orderby b.id descending
                       select new MoveHisModel
                       {
                           id = b.id,
                           moveDate = b.movedate,
                           fromGraveId = b.fromgraveid,
                           fromGraveName = b.tbGraveFrom.name,
                           toGraveId = b.tograveid,
                           togGaveName = b.tbGraveTo.name,
                           deadId = b.deadid,
                           deadName = b.tbDead!=null ? b.tbDead.name : "",
                           deadArea = b.tbDead != null ? b.tbDead.tbArea.name : "",
                           remark = b.remark,
                           userId = b.userid,
                           userName = b.tbUser.userName
                       };
            string json = JsonUtil.ToJson(list.ToList());
            return json;
        }

        public string search(int pageSize, int pageIndex, string order, string where)
        {
            int totalCount = 0;
            if (user.roleNames.Contains("镇级管理员") || user.roleNames.Contains("骨灰堂管理员"))
            {
                string s = "";
                user.paradiseIds.ForEach(m => { s += m + ","; });
                s = s.Trim(',');
                where = string.Format(" (fromgraveid in (select id from tbGrave where paradiseId in ({0}))) and ", s) + where;
            }
            SqlParameter paramTotalCount = new SqlParameter("@totalCount", totalCount);
            paramTotalCount.Direction = ParameterDirection.Output;
            var pager = DB.tbMoveHis.SqlQuery("exec sp_Pager @tableName,@columns,@order,@pageSize,@pageIndex,@where,@totalCount output",
                GetPagerParameters("tbMoveHis", "*", order, where, pageSize, pageIndex, paramTotalCount)).ToList();
            var list = from b in pager
                       orderby b.id descending
                       select new MoveHisModel
                       {
                           id = b.id,
                           moveDate = b.movedate,
                           fromGraveId = b.fromgraveid,
                           fromGraveName = b.tbGraveFrom.tbParadise.name+ b.tbGraveFrom.name,
                           toGraveId = b.tograveid,
                           togGaveName = b.tbGraveTo!=null ? b.tbGraveTo.tbParadise.name + b.tbGraveTo.name : "",
                           deadId = b.deadid,
                           deadName = b.tbDead != null ? b.tbDead.name : "",
                           deadArea = b.tbDead != null ? b.tbDead.tbArea.name : "",
                           remark = b.remark,
                           userId = b.userid,
                           userName = b.tbUser.userName
                       };
            var l = list.ToList();
            totalCount = Convert.ToInt32(paramTotalCount.Value);
            string json = JsonUtil.ToJson(new { total = totalCount, rows = l });
            return json;
        }

        public string delete(string strIds)
        {
            List<int> ids = new List<int>();
            if (!string.IsNullOrEmpty(strIds))
            {
                string[] arr = strIds.Split(',');
                foreach (var id in arr)
                {
                    ids.Add(int.Parse(id));
                }
            }
            var logs = DB.tbOperateLog.Where(m => ids.Contains(m.id)).ToList();
            if (logs != null)
            {
                logs.ForEach(m => DB.tbOperateLog.Remove(m));
                DB.SaveChanges();
            }
            return JsonUtil.ToJson(new { success = true });
        }
    }
}